﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bus.Bll;
using Bus.Model;

public partial class admin_typecount : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitCountData();
        }
    }
    private void InitCountData()
    {
        try
        {
            lao.Text = Bus.Dal.DbHelperSQL.GetSingle("select count(*) as cnt from tb_custom where ctype=0").ToString();
            cheng.Text = Bus.Dal.DbHelperSQL.GetSingle("select count(*) as cnt from tb_custom where ctype=1").ToString();
            xs.Text = Bus.Dal.DbHelperSQL.GetSingle("select count(*) as cnt from tb_custom where ctype=2").ToString();
            cntsum.Text = Bus.Dal.DbHelperSQL.GetSingle("select sum(vl) as cnt from v_count ").ToString();
            cus.Text = Bus.Dal.DbHelperSQL.GetSingle("select sum(money) as price from tb_cost").ToString();


            day.Text = Bus.Dal.DbHelperSQL.GetSingle("select sum(vl) as vl from v_count  where DATENAME (yyyy ,GETDATE ())+DATENAME (MM,getdate())+DATENAME (DD,getdate())=DATENAME (yyyy,costdate)+DATENAME (MM,costdate)+DATENAME (DD,costdate)").ToString();
        }
        catch (Exception e)
        {
            day.Text = "0";
        }

        //统计视图中的数据
        tpcnt.DataSource = Bus.Dal.DbHelperSQL.Query("select ctype,sum(vl) as sm ,avg(vl) as vg,count(*) as cnt from v_count group by ctype");
        tpcnt.DataBind();
    }
    public string GetTypestr(string t)
    {
        string tep=string.Empty;
        if (int.Parse(t) == 0)
            tep = "老年人";
        if (int.Parse(t) == 1)
            tep = "成年人";
        if (int.Parse(t) == 2)
            tep = "学生";
        return tep;
    }
    protected void ddl_Changed(object sender, EventArgs e)
    {
        if (ddl.SelectedItem.Value == "4")
        {
            dpl.Visible = true;
            //lbl_Date.Visible = true;
            //txt_Date.Visible = true;
            //to.Visible = true;
        }
        else
        {
            txt_Date.Text = "";
            to.Text = "";
            dpl.Visible = false;
            //lbl_Date.Visible = false;
            //txt_Date.Visible = false;
            //to.Visible = false;
           
        }
    }
    protected void btnje_Click(object sender, EventArgs e)
    {
        if (ddl.SelectedItem.Value == "-1")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择统计项！');</script>");
            return;
        }
        string strfilter = string.Empty;
        string cntSql = "select ctype,sum(vl) as sm ,avg(vl) as vg,count(*) as cnt from v_count";
        if (ddl.SelectedItem.Value == "0")
        {
            //日营业额
            cntSql += " where DATENAME (yyyy ,GETDATE ())+DATENAME (MM,getdate())+DATENAME (DD,getdate())=DATENAME (yyyy,costdate)+DATENAME (MM,costdate)+DATENAME (DD,costdate)";
            strfilter = " where DATENAME (yyyy ,GETDATE ())+DATENAME (MM,getdate())+DATENAME (DD,getdate())=DATENAME (yyyy,costdate)+DATENAME (MM,costdate)+DATENAME (DD,costdate)";
        }
        else if (ddl.SelectedItem.Value == "1")
        {
            //月营业额
            cntSql += " where DATENAME (yyyy ,GETDATE ())+DATENAME (MM,getdate())=DATENAME (yyyy ,costdate)+DATENAME (MM,costdate)";
            strfilter = " where DATENAME (yyyy ,GETDATE ())+DATENAME (MM,getdate())=DATENAME (yyyy ,costdate)+DATENAME (MM,costdate)";
        }
        else if (ddl.SelectedItem.Value == "2")
        {
            //年营业额
            cntSql += " where  DATENAME (yyyy ,GETDATE ())= DATENAME (yyyy ,costdate)";
            strfilter = " where  DATENAME (yyyy ,GETDATE ())= DATENAME (yyyy ,costdate)";
        }
        else if (ddl.SelectedItem.Value == "3")
        {
            cntSql += " ";
        }
        else
            if (ddl.SelectedItem.Value == "4")
            {
                // txt_Date.Visible = true;
                string date = txt_Date.Text;
                string todate = to.Text;
                if (date == "" && todate == "")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择文本框内时间值！');</script>);");
                    return;
                }
                else
                {
                    cntSql += " where convert(varchar(10),[costdate],120)>='" + date + "' and convert(varchar(10),[costdate],120)<='" + todate + "'";
                    strfilter = " where convert(varchar(10),[costdate],120)>='" + date + "' and convert(varchar(10),[costdate],120)<='" + todate + "'";
                }
            }
        //全部营业额不需要加条件
        cntSql += " group by ctype ";
        tpcnt.DataSource = Bus.Dal.DbHelperSQL.Query(cntSql);
        tpcnt.DataBind();

        try
        {
            day.Text = Bus.Dal.DbHelperSQL.GetSingle("select sum(vl) as vl from v_count " + strfilter).ToString();
        }
        catch (Exception eee)
        {
            day.Text = "0";
        }
    }
}
